package game.model.impl

import game.Config
import game.business.*
import game.enums.Direction
import game.model.View
import org.itheima.kotlin.game.core.Painter
import kotlin.random.Random

/**
 * 敌方坦克可以移动(避开阻挡物)
 * 敌方坦克可以自动移动（自己动起来）
 * 敌方坦克可以相互阻挡
 * 敌方坦克可以自动射击
 * 敌方坦克可以销毁
 */
class Enemy(override var x: Int, override var y: Int) :
        Movable, AutoMove, BlockAble, AutoShot , SufferAble,DestroyAble{

    override var currentDirection: Direction = Direction.down

    override val speed: Int = Config.tankSpeed
    override var width: Int = Config.block
    override var height: Int= Config.block

    private var badDirection: Direction? = null

    private var lastShotTime = 0L
    private var shotFrequency = 800

    private var lastMoveTime = 0L
    private var MoveFrequency = 50

    override var blood: Int = 10

    override fun draw() {
        var imagePath = when (currentDirection) {
            Direction.up -> "img/enemy_1_u.gif"
            Direction.down -> "img/enemy_1_d.gif"
            Direction.left -> "img/enemy_1_l.gif"
            Direction.right -> "img/enemy_1_r.gif"
        }
        Painter.drawImage(imagePath, x, y)
    }

/*    override fun willCollision(block: BlockAble): Direction? {
        var collision = checkCollision(block)
        return if (collision) currentDirection else null
    }*/

    override fun notifyCollision(direction: Direction?, block: BlockAble?) {
        this.badDirection = direction
    }

    override fun autoMove() {

        //移动频率
        var current = System.currentTimeMillis()
        if (current - lastMoveTime < MoveFrequency)return
        lastMoveTime = current


        if (currentDirection == this.badDirection){
            currentDirection = randomDirection(badDirection)
            return
        }


        when (currentDirection) {
            Direction.up -> y -= speed
            Direction.down -> y += speed
            Direction.left -> x -= speed
            Direction.right -> x += speed
        }

        if (x < 0) x = 0
        if (x > Config.gameWidth - width) x = Config.gameWidth - width
        if (y < 0) y = 0
        if (y > Config.gameHeight - height) y = Config.gameHeight - height
    }

    /**
     * 产生一个随机的方向
     */
    private fun randomDirection(bad: Direction?):Direction{
        var i = Random.nextInt(4)
        var direction = when (i) {
            0 -> Direction.up
            1 -> Direction.down
            2 -> Direction.left
            3 -> Direction.right
            else -> Direction.up
        }
        if (direction == bad){
            return randomDirection(bad)
        }
        return direction;
    }

    override fun autoShot(): View? {

        var current = System.currentTimeMillis()
        if (current - lastShotTime < shotFrequency)return null
        lastShotTime = current

        var x = this.x
        var y = this.y
        when(currentDirection){
            Direction.up -> {
                x += Config.block / 2 - 7
                y -= Config.block / 2 - 8
            }
            Direction.down -> {
                x += Config.block / 2 - 7
                y += Config.block - 8
            }
            Direction.right -> {
                x += Config.block - 8
                y += Config.block / 2  - 8
            }
            Direction.left -> {
                y += Config.block / 2 - 8
                x -= Config.block / 2 - 8
            }
        }
         return  Bullet(x,y,currentDirection,this)
    }

    override fun notifySuffer(attackAble: AttackAble): Array<View>? {
        //敌人打敌人不掉血
        if (attackAble.source is Enemy){
            return null
        }
        blood -= attackAble.attackPower
        return arrayOf(Blast(x, y))
    }

    override fun isDestroyed(): Boolean=blood <= 0

}